package com.ligongmusic.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.ligongmusic.R;
import com.ligongmusic.fragment.Dynamic;
import com.ligongmusic.fragment.Personal;
import com.ligongmusic.fragment.Ranking;
import com.ligongmusic.fragment.UploadMusic;
import com.ligongmusic.fragment.UploadMusicUP;
import com.ligongmusic.service.MusicOnlineService;
import com.ligongmusic.util.ActivityManager;
import com.ligongmusic.util.GlobalParams;
import com.ligongmusic.view.TopManager;

public class MainActivity extends FragmentActivity implements OnCheckedChangeListener, OnPageChangeListener {
	private RadioGroup mRadioGroup;
	private ViewPager mViewPager;
	private static long startTime = 0;
	private static long endTime = 0;
	private List<Fragment> homePager = new ArrayList<Fragment>();
	private int checked;
//	public boolean isChanged = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.base_activity);
//		ActivityManager.startActivity(MainActivity.this, MusicActivity.class);
		initView();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
//		mRadioGroup.clearCheck();
//		mRadioGroup.check(checked);
		int position = mViewPager.getCurrentItem();
		TopManager.getTopManager(this).showTop(false, position == 0 ? "音乐动态" : position == 1 ? "排行榜" : position == 2 ? "上传音乐" : "我", null);
	}

	private void initView() {
		// 动态
		homePager.add(new Dynamic());
		// 排行 
		homePager.add(new Ranking());
		// 上传界面
		homePager.add(new UploadMusic());
		// 个人中心界面
		homePager.add(new Personal());
		mRadioGroup = (RadioGroup) findViewById(R.id.rg_navigation);
		mViewPager = (ViewPager) findViewById(R.id.content_viewpager);
		mViewPager.setOffscreenPageLimit(1);
		mRadioGroup.setOnCheckedChangeListener(this);
		mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public int getCount() {
				return homePager.size();
			}
			@Override
			public Fragment getItem(int arg0) {
				return homePager.get(arg0);
			}

			@Override
			public void destroyItem(ViewGroup container, int position, Object object) {
//				super.destroyItem(container, position, object);
			}
		});
		mViewPager.setOnPageChangeListener(this);
		// 设置默认选中动态
		((RadioButton)findViewById(R.id.rb_dynamic)).setChecked(true);
//		mRadioGroup.check(R.id.rb_dynamic);
		// 保存选中的item
		checked = mRadioGroup.getCheckedRadioButtonId();
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		GlobalParams.isChanged = true;
		// 改变选择
		switch (checkedId) {
		case R.id.rb_dynamic:
			if(Math.abs(mViewPager.getCurrentItem() - 0) > 1)
				mViewPager.setCurrentItem(0, false);
			else 
				mViewPager.setCurrentItem(0);
			TopManager.getTopManager(this).showTop(false, "音乐动态", null);
			break;
		case R.id.rb_ranking:
			if(Math.abs(mViewPager.getCurrentItem() - 1) > 1)
				mViewPager.setCurrentItem(1, false);
			else 
				mViewPager.setCurrentItem(1);
			TopManager.getTopManager(this).showTop(false, "排行榜", null);
			break;
		case R.id.rb_upload:
			if(Math.abs(mViewPager.getCurrentItem() - 2) > 1)
				mViewPager.setCurrentItem(2, false);
			else 
				mViewPager.setCurrentItem(2);
			TopManager.getTopManager(this).showTop(false, "上传音乐", null);
			break;
		case R.id.rb_persional_center:
			if (!GlobalParams.isLogin){
				Intent intent = new Intent(MainActivity.this, UserLogin.class);
				startActivityForResult(intent, 0x7);
			}else {
				if (Math.abs(mViewPager.getCurrentItem() - 3) > 1)
					mViewPager.setCurrentItem(3, false);
				else
					mViewPager.setCurrentItem(3);
				TopManager.getTopManager(this).showTop(false, "我", null);
			}
			break;
		}
		// 保存选中的item
//		if (GlobalParams.isLogin && mRadioGroup.getCheckedRadioButtonId() != R.id.rb_persional_center)
//			checked = mRadioGroup.getCheckedRadioButtonId();
 		if (GlobalParams.isLogin)
			checked = mRadioGroup.getCheckedRadioButtonId();
		if (!GlobalParams.isLogin && mRadioGroup.getCheckedRadioButtonId() != R.id.rb_persional_center)
			checked = mRadioGroup.getCheckedRadioButtonId();

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onPageSelected(int current) {
		GlobalParams.isChanged = true;
		if (current == 3 && !GlobalParams.isLogin){
			Intent intent = new Intent(MainActivity.this, UserLogin.class);
			startActivityForResult(intent, 0x7);
		}else {
			// 界面改变完成
			int id = current == 0 ? R.id.rb_dynamic : current == 1 ? R.id.rb_ranking : current == 2 ? R.id.rb_upload : R.id.rb_persional_center;
			((RadioButton)findViewById(id)).setChecked(true);
//			mRadioGroup.check(current == 0 ? R.id.rb_dynamic : current == 1 ? R.id.rb_ranking : current == 2 ? R.id.rb_upload : R.id.rb_persional_center);
			// 保存选中的item
			checked = mRadioGroup.getCheckedRadioButtonId();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if (startTime == 0) {
	       	 	startTime = System.currentTimeMillis();
	            endTime = 0;
	            Toast.makeText(MainActivity.this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
			} else {
				endTime = System.currentTimeMillis();
				if (endTime - startTime < 1000) {
					Intent intent = new Intent(MainActivity.this, MusicOnlineService.class);
					intent.putExtra("status", MusicOnlineService.EXIT_PLAYER);
					startService(intent);
	           		finish();
				} else {
					startTime = System.currentTimeMillis();
	                endTime = 0;
	                Toast.makeText(MainActivity.this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
				}
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == 0x1 || requestCode == 0x2 || requestCode == 0x3){
			homePager.get(2).onActivityResult(requestCode, resultCode, data);
		}else if(requestCode == 0x4 && resultCode == 0x1){ // 个人中心
			homePager.get(3).onActivityResult(requestCode, resultCode, data);
		}else if(requestCode == 0x6 && resultCode == 0x5){
			homePager.get(0).onActivityResult(requestCode, resultCode, data);
		}else if(requestCode == 0x6 && resultCode == 0x6){
			homePager.get(0).onActivityResult(requestCode, resultCode, data);
		}else {
			if (requestCode == 0x7 && resultCode == 0x7) {
				RadioButton button = (RadioButton) findViewById(R.id.rb_persional_center);
				button.setChecked(true);
//				mRadioGroup.check(R.id.rb_persional_center);
				mViewPager.setCurrentItem(3);
				GlobalParams.isRefreshRanking = true;
				GlobalParams.isRefreshDynamic = true;
			} else if (requestCode == 0x7 && resultCode == 0x8) {
				int current = Integer.parseInt(findViewById(checked).getTag().toString());
				RadioButton rButton= (RadioButton) findViewById(checked);
				rButton.setChecked(true);
//				mRadioGroup.check(checked);
				mViewPager.setCurrentItem(current);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
	}
}
